import os
import json
def get_json_var(file_name, key):
    file = open(file_name, 'r', encoding='utf-8')
    if file is None:
        return None
    data =  json.load(file);
    if data is None:
        return None
    return data[key]


def load_json_to_class(data, target_class):
    """将JSON字符串转换为指定类的实例"""
    return target_class(**data)

def traverse_folders(root_dir):
    dirpaths = []
    for dirpath, dirnames, filenames in os.walk(root_dir):
        if root_dir != dirpath:
            dirpaths.append(dirpath)
        # print(f"当前目录: {dirpath}")
        # print(f"子目录: {dirnames}")
        # print(f"文件: {filenames}")
    return dirpaths, filenames

def decrypt_m4s(input_path, output_path):
    with open(input_path, 'rb') as f:
        data = f.read()
    # 删除前9字节的0
    data = data[9:]
    # 替换特定字符（如$ -> 空格）
    # data = data.replace(b'$', b' ')
    # # 删除avc1标识（可选）
    # data = data.replace(b'avc1', b'')
    with open(output_path, 'wb') as f:
        f.write(data)

